//
//  MarketViewController.m
//  Futures
//
//  Created by Cheng on 2017/6/22.
//  Copyright © 2017年 Cheng. All rights reserved.
//

#import "MarketViewController.h"
#define kItemW ([UIScreen mainScreen].bounds.size.width - 3 * kItemMargin) / 2
#define kItemH 44
#define kItemMargin 10
#define kMarketCellID @"kMarketCellID"
#import "MarketCell.h"


@interface MarketViewController () <UICollectionViewDataSource,UICollectionViewDelegate>{
    NSArray *_titles;
    NSTimer *mTimer;
}

@property (strong,nonatomic) UICollectionView *collectionView;

@end

@implementation MarketViewController

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        // 1.创建布局
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(kItemW, kItemH);
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = kItemMargin;
        layout.sectionInset = UIEdgeInsetsMake(0,kItemMargin,0,kItemMargin);
        // 2.创建UICollectionView
        CGRect frame = self.view.bounds;
//        frame.size.height = frame.size.height - 64 - 44 - 44;
        _collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor blackColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        [_collectionView registerNib:[UINib nibWithNibName:@"MarketCell" bundle:nil] forCellWithReuseIdentifier:kMarketCellID];
    }
    return _collectionView;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self startTimer];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    [self stopTimer];
}

- (void)startTimer // 30秒刷一次
{
    if (mTimer == nil) { // 30秒刷一次
        mTimer = [[NSTimer alloc] initWithFireDate:[NSDate date] interval:1.0f target:self selector:@selector(processEvent) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:mTimer forMode:NSDefaultRunLoopMode];
        
    }
}

- (void)stopTimer
{
    [mTimer invalidate];
    mTimer = nil;
    
}

- (void)processEvent{
    [self.collectionView reloadData];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    _titles = @[@"最新",@"今开",@"涨跌",@"最高",@"涨幅",@"最低",@"总量",@"昨收"];
    
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"MarketCell" bundle:nil] forCellWithReuseIdentifier:kMarketCellID];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _titles.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MarketCell *cell = nil;
    
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:kMarketCellID forIndexPath:indexPath];
    
    QuoteModel *model = [Global shareInstance].quoteModel;
    
    cell.title.text = _titles[indexPath.item];
    
    
    
    if (model.ChangeRate> 0) {
        cell.value.textColor = [UIColor redColor];
    }else{
        cell.value.textColor = [UIColor greenColor];
    }
    
    NSString *value;
    
    switch (indexPath.item) {
        case 0:
            value = NSStringFromFloat(model.LastPrice);
            break;
        case 1:
            value = NSStringFromFloat(model.open);
            break;
        case 2:
            value = NSStringFromFloat(model.Change);
            break;
        case 3:
            value = NSStringFromFloat(model.high);
            break;
        case 4:
            value = [NSStringFromFloat(model.ChangeRate) stringByAppendingString:@"%"];
            break;
        case 5:
            value = NSStringFromFloat(model.low);
            break;
        case 6:
            value = NSStringFromInt(model.Vol);
            cell.value.textColor = [UIColor yellowColor];
            break;
        case 7:
            value = NSStringFromFloat(model.Lastclose);
            cell.value.textColor = [UIColor whiteColor];
            break;
            
        default:
            break;
    }
    
    cell.value.text = value;
    
    
    
    return cell;
}



@end
